import CameraControl from "../Camera/CameraControl";
import LevelObjBase from "./LevelObjBase";
import GamePanel from "../Panel/GamePanel";
import { LevelData } from "../Data/LevelData";
import { GamePropData } from "../Data/GamePropData";
import GamePropUi from "../UI/GamePanel/GamePropUi";
import EnumData from "../../Enum/EnumData";
import CommonConstant from "../../constant/CommonConstant";
import { utils } from "../../utils/CommonUtil";
import Sound from "../../constant/SoundNameConstant";
import RoomControl from "./RoomControl";
import GamePropConst from "../../constant/GamePropConst";
import PropMessagePanel from "../Panel/PropMessagePanel";
import { DialogData } from "../Data/DialogData";
import LevelInitBase from "./LevelInitBase";

export default class LevelBaseControl extends Laya.Script3D {

    /** 记录本关开始时间*/
    public startTime: number;

    /** 相机*/
    protected cameraControl: CameraControl;

    /** 当前相机前的模块物体*/
    private curLevelObj: LevelObjBase;

    /** 模块root*/
    public levelObjRoot: Laya.Sprite3D;

    /** 该关所有的模块*/
    protected allLevelObjs: Array<LevelObjBase>;

    /** 游戏内ui面板*/
    private gamePanel: GamePanel;

    /** 当前关卡 已经生成的非可获取 3d渲染物体(因为可获取的直接获得后就消失了 不会再进行3D展示 而不可获取的3D物体可能会再次渲染 而且需要保留上次查看的状态)*/
    curRenderObjDic: Laya.WeakObject = new Laya.WeakObject();//<string, GameObject>

    /** 本关获得的纪念品数*/
    public curMemorialNum: number = 0;
    /** 当前关卡数据*/
    private curLevelData: LevelData;

    private mowner: Laya.Sprite3D;

    public LevelInit: LevelInitBase;

    /** 当前选中的道具*/
    public get CurSelectProp(): GamePropData {

        let curSelectUi: GamePropUi = this.gamePanel.curClickPropUi;
        if (curSelectUi == null) {
            return null;
        }
        else {
            return curSelectUi.curData;
        }
    }

    /** 有没有选择对应的道具*/
    public IsSelectProp(propId: number): boolean {
        let curSelectData: GamePropData = this.CurSelectProp;
        if (curSelectData != null && curSelectData.id == propId) {
            return true;
        }
        else {
            return false;
        }

        
    }

    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;

        /** 设置当前关卡*/
        window.gameFacade.LevelMng.SetCurLevel(this);

        this.initComponent();
        this.startByBase()
    }

    protected startByBase() {

    }

    private initComponent() {

        /** 创建游戏内面板*/
        this.gamePanel = window.gameFacade.SceneMng.GetPanelById(EnumData.EnumPanelType.GamePanel) as GamePanel;
        this.cameraControl = this.gamePanel.CameraControl;
        this.cameraControl.CurLevelControl = this;
        this.levelObjRoot = (this.mowner.getChildByName("levelObjs") as Laya.Sprite3D);
        this.allLevelObjs = utils.CommonUtils.GetComponentsInDirectChildren<LevelObjBase>(this.levelObjRoot, LevelObjBase);
        this.startTime = Laya.startTimer.currTimer;
        this.curLevelData = window.gameFacade.LevelMng.GetCurLevelData();

        for (let index = 0; index < this.allLevelObjs.length; index++) {
            let item = this.allLevelObjs[index];
            item.owner.active = false;
        }
        /** 先显示第一个模块*/
        this.ChangeCurLevelObj(this.allLevelObjs[0], EnumData.CameraMoveEff.None, false);
        this.ShowOpenDialog();
        this.PlayBgmIn();

    }

    public PlayBgmIn(): void {
        /** 播放bgm*/
        window.gameFacade.SoundMng.playMusic(Sound.soundBgArr.BGM_IN);
    }

    /** 获得相机*/
    public GetCameraControl(): CameraControl {
        return this.cameraControl;
    }

    /** 获得当前相机前的模块物体*/
    public GetCurLevelObj(): LevelObjBase {
        return this.curLevelObj;
    }

    /** 切换模块*/
    public ChangeCurLevelObj(
        levelObj: LevelObjBase,
        cameraMoveEff: EnumData.CameraMoveEff = EnumData.CameraMoveEff.None,
        isPlayVoice: boolean = true): void {
        if (this.cameraControl.curMoveStack.Count > 0 || this.cameraControl.IsMovingCamera()) {
            return;
        }

        if (isPlayVoice) {
            window.gameFacade.SoundMng.playSound(Sound.soundClipArr.STEP);
        }
        /** 把现在相机里的模块移出相机*/
        if (this.curLevelObj != null) {
            this.curLevelObj.MoveOutCamera();
        }
        this.curLevelObj = levelObj;
        /** 新模块进入相机*/
        levelObj.MoveInCamera();

        /** 刷新墙*/
        if (levelObj instanceof RoomControl) {
            let roomControl = levelObj.owner.getComponent(RoomControl) as RoomControl;
            roomControl.RefreshWall();
        }

        this.cameraControl.DoMoveEff(cameraMoveEff);
    }

    /** 执行交互物品逻辑 参数为物体名字*/
    public DoInteractObjLogic(_name: string): boolean {
        return true;
    }

    public GetGameProp(propData: GamePropData): void {
        /** 如果是可以获得的道具 就获得*/
        if (propData.isCanGet) {
            console.log("获得道具： " + propData._nameKey);

            /** 是游戏内用的道具 就显示在道具栏上*/
            if (propData.propType == GamePropConst.IN_GAME_PROP_TYPE) {
                window.gameFacade.SoundMng.playSound(Sound.soundClipArr.GET);
                this.gamePanel.GetNewProp(propData);
            }
            /** 如果是收集类的道具 就不显示在道具栏*/
            else if (propData.propType == GamePropConst.COLLCT_PROP_TYPE) {
                window.gameFacade.SoundMng.playSound(Sound.soundClipArr.GET);
                this.AddMemorialNum();
            }
            else {

            }
        }
        // let lastPropMessagePanel =  window.gameFacade.SceneMng.GetPanelById(EnumData.EnumPanelType.PropMessagePanel);
        // if(lastPropMessagePanel){
        //     window.gameFacade.SceneMng.close(EnumData.EnumPanelType.PropMessagePanel);
        // }

        /** 打开显示道具信息的面板*/
        let propMessagePanel = window.gameFacade.SceneMng.Create
            (EnumData.EnumPanelType.PropMessagePanel, propData) as PropMessagePanel;
        propMessagePanel.InitUi(propData);
    }

    /** 增加纪念品数*/
    private AddMemorialNum(): void {
        this.curMemorialNum++;
        /** 刷新ui上显示的数量*/
        this.gamePanel.RefreshMemorial(this.curMemorialNum);
    }

    /** 获得道具 参数为id*/
    public GetGamePropById(propId: number) {
        let propData: GamePropData = window.gameFacade.ConfigMng.GetGamePropDataById(propId);
        this.GetGameProp(propData);
    }
    /** 使用当前选中的道具*/
    public UseCurProp(): void {
        /** 播放使用道具 音效*/
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.USE);
        this.gamePanel.UseCurProp();
    }
    /** 使用道具*/
    public UseProp(usePropDatas: Array<GamePropData>) {
        this.gamePanel.UseProp(usePropDatas);
    }


    public ShowDialog(dialogId: number): void {
        let data: DialogData = window.gameFacade.ConfigMng.GetDialogDataById(dialogId);
        let datas: Array<DialogData> = new Array<DialogData>();
        if (data != null) {
            datas.push(data);
        }
        this.gamePanel.TypeDialog(datas);
    }

    /** 显示提示dialog*/
    public ShowDialogByDialogDatas(dialogDatas: Array<DialogData>) {
        this.gamePanel.TypeDialog(dialogDatas);
    }

    /** 显示关卡一开始的对话*/
    public ShowOpenDialog(): void {
        let openLevelDialogs: Array<DialogData> = window.gameFacade.ConfigMng.GetOpenLevelDialogDatas(this.curLevelData.id);
        this.ShowDialogByDialogDatas(openLevelDialogs);
    }

    /** 过关*/
    public FinishLevel(): void {
        /** 根据当前获得的纪念品 设置本关获得的最多纪念品数*/
        this.curLevelData.SetCurMostMemorial(this.curMemorialNum);
        window.gameFacade.SceneMng.Create(EnumData.EnumPanelType.CompletePanel);
    }

    // onDestroy() {
    //     console.log("卸载摄像机");
    //     Laya.loader.clearRes(CommonConstant._cameraRotatePrefab);
    // }
}